<%@page import="com.javandad.app.server.db.DatabaseManager"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="com.javandad.app.server.db.PMFSingleton"%>
<%@page import="java.util.List"%>
<%@page import="java.util.LinkedList"%>
<%@page import="com.javandad.app.shared.entity.project.Project"%>
<%@page import="com.javandad.app.shared.entity.project.ProjectAssoc"%>
<%@page import="com.javandad.app.shared.entity.*"%>

<%!DatabaseManager dbm = DatabaseManager.getDefaultDatabaseManager();

	class Item {
		Item(String name, int number) {
			this.name = name;
			this.number = number;

		}

		String name;
		String display;
		int number;

	}
	
	
	%>
	<%
	 PersistenceManager pm = PMFSingleton.getPM();
 	User user = (User)session.getAttribute("user");
 	String codesvrParam = request.getParameter("codesvrParam");
	
	%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="Javandad.css" rel="stylesheet" type="text/css">
<title>JaVandad</title>
<script type="text/javascript" charset="CDATA" language="javascript"
	src="login/login.nocache.js"> </script>
</head>

<body style="font-family: sans-serif;">
<iframe src="javascript:''" id="__gwt_historyFrame"
	style="position: absolute; width: 0; height: 0; border: 0"></iframe>


<div
	style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<table style="width: 100%; height: 100%;">
	<tr height="5%">
		<td style="background-color: #707dbe;">
		<table width="100%" height="100%" border=0>
			<tr>
				<td valign="center">
				<table align="left" width="100%">
					<tr valign='center'>
					<td style="vertical-align: top"><a href=""><img alt="Vandad Logo"
							src="img/logo.jpg" width="200" height="50" /></a></td>
							<td  style="white-space: nowrap;"><h2>A Place For Social Software Development </h2></td>
						<td align="right"
							style="vertical-align: top; text-align: right; width: 100%;">
						<jsp:include page="jspmodules/headerMenu.jsp">
						<jsp:param value="<%= codesvrParam %>" name="codesvrParam"/>
						</jsp:include>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</td>
	</tr>

	<tr  height="85%">



		<td width="100%" valign="top">
		<table width="100%" height="100%" border="0">
			<tr height="100%" align="left">
				<td width="80%" valign="top">
				<table width="100%" height="100%" border="0">
					<tr align="center" height="50%">
						<td valign="top" width="33%" bgcolor="edecf6"><b>Most
						Owned Projects</b> <%
 	final int COUNT = 15;

 	// represents a project with number of owners
  
 	
 	LinkedList<Item> pa = new LinkedList<Item>();
 	List<Project> projects = dbm.getProjects(pm);

 	// get projects with number of owners
 	for (Project project : projects) {
 		List<User> owners = dbm.getAssociatedUsers(project.getName(),
 				ProjectAssoc.OWNING_PROJECT, pm);
 		pa.add(new Item(project.getName(), owners.size()));
 	}

 	// sort by number of owners
 	Item temp;
 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}

 	}
 %>
						<table width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							<tr>
								<td><%=(k + 1) + ".  "%><a
									href='project.jsp?<%=codesvrParam + "&" + "name="
						+ pa.get(k).name%>'>
								<%=pa.get(k).name + "( " + pa.get(k).number + " )"%> </a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>
						<td  valign="top" width="33%"><b>Most Participated
						Projects</b> <%
 	pa.clear();
 	for (Project project : projects) {
 		pa.add(new Item(project.getName(), dbm.getAssociatedUsers(
 				project.getName(), ProjectAssoc.PARTICIPATING_PROJECT,
 				pm).size()));
 	}

 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}
 	}
 %>
						<table width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							<tr>
								<td><%=(k + 1) + ".  "%><a
									href='project.jsp?<%=codesvrParam + "&" + "name="
						+ pa.get(k).name%>'>
								<%=pa.get(k).name + "( " + pa.get(k).number + " )"%> </a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>
						<td  valign="top" width="33%" bgcolor="edecf6"><b>Most
						Followed Projects</b> <%
 	pa.clear();
 	for (Project project : projects) {
 		pa.add(new Item(project.getName(), dbm.getAssociatedUsers(
 				project.getName(), ProjectAssoc.FOLLOWING_PROJECT, pm)
 				.size()));
 	}

 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}
 	}
 %>
						<table width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							<tr>
								<td><%=(k + 1) + ".  "%><a
									href='project.jsp?<%=codesvrParam + "&" + "name="
						+ pa.get(k).name%>'><%=pa.get(k).name + "( " + pa.get(k).number + " )"%>
								</a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>
					</tr>
					<tr align="center" height="50%">
						<td valign="top" width="33%"><b>Most Owning Users</b> <%
 	pa.clear();
 	List<User> users = dbm.getUsers(pm);
 	for (User u : users) {
 		int size = dbm.getAssociatedProjects(u.getUsername(),
 				ProjectAssoc.OWNING_PROJECT, pm).size();
 		if (size > 0)
 			pa.add(new Item(u.getUsername(), size));
 	}

 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}
 	}
 %>
						<table width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							
							
							
							<tr>   
								<td><%=(k + 1) + ".  "%><a
									href='home.jsp?<%=codesvrParam + "&" + "view="
						+ pa.get(k).name%>'>
								<%=pa.get(k).name + "( " + pa.get(k).number + " )"%> </a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>
						<td valign="top" width="33%" bgcolor="edecf6"><b>Most
						Participating Users</b> <%
 	pa.clear();
 	users = dbm.getUsers(pm);
 	for (User u : users) {
 		int size = dbm.getAssociatedProjects(u.getUsername(),
 				ProjectAssoc.PARTICIPATING_PROJECT, pm).size();
 		if (size > 0)
 			pa.add(new Item(u.getUsername(), size));
 	}

 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}
 	}
 %>
						<table width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							<tr>
								<td><%=(k + 1) + ".  "%><a
									href='home.jsp?<%=codesvrParam + "&" + "view="
						+ pa.get(k).name%>'><%=pa.get(k).name + "( " + pa.get(k).number + " )"%>
								</a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>
						<td  valign="top" width="33%"><b>The Biggest Groups</b> <%
 	pa.clear();
 	List<UserGroup> userGroups = dbm.getUserGroups(pm);
 	for (UserGroup ug : userGroups) {
 		int size = dbm.getMembers(ug, pm).size();
 		if (size > 0)
 			pa.add(new Item(ug.getGroupURL(), size));
 	}

 	for (int i = 0; i < COUNT && i < pa.size(); ++i) {
 		for (int j = i; j < pa.size(); ++j) {
 			if (pa.get(i).number < pa.get(j).number) {
 				temp = pa.get(i);
 				pa.set(i, pa.get(j));
 				pa.set(j, temp);
 			}
 		}
 	}
 %>
						<table    width='100%' align='left'>
							<%
								for (int k = 0; k < pa.size() && k < COUNT; ++k) {
							%>
							<tr>
								<td><%=(k + 1) + ".  "%><a
									href='group.jsp?<%=codesvrParam + "&" + "group="
						+ pa.get(k).name%>'>
								<%=pa.get(k).name + "( " + pa.get(k).number + " )"%> </a></td>
							</tr>
							<%
								}
							%>
						</table>
						</td>


					</tr>
				</table>
				</td>
				<%@ include file="jspmodules/search.jsp"%>
			</tr>


		</table>
		</td>
	</tr>
	<%@ include file="jspmodules/bottom.jsp"%>
</table>

</div>
</body>
</html>

